package st.business.tepth.business.user.busnumber.workholder.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.alibaba.fastjson.TypeReference;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import io.reactivex.disposables.Disposable;
import st.business.tepth.R;
import st.business.tepth.business.bean.CheckResultBean;
import st.business.tepth.config.Url;
import st.business.tepth.manager.UserManager;
import st.shadow.library.core.net.callback.IError;
import st.shadow.library.core.net.callback.ISuccess;
import st.shadow.library.core.net.data.ObjConverter;
import st.shadow.library.core.net.rx.RxRestClient;
import st.shadow.library.core.util.utilcode.util.ScreenUtils;
import st.shadow.library.ui.recycler.BaseDecoration;

/**
 * @author zhangyu
 * @date 2019/5/13
 */
public class DialogCheckResult extends Dialog {
    private Disposable mDisposable;

    public DialogCheckResult(Context context, int repairId) {
        super(context);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = LayoutInflater.from(context).
                inflate(R.layout.dialog_check_result, null);
        final RecyclerView recyclerView = view.findViewById(R.id.recycler_view_result);
        view.findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        final ResultAdapter adapter = new ResultAdapter(R.layout.item_check_result, null);
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(BaseDecoration.
                create(Color.parseColor("#f0f0f0"), 1));
        adapter.setEmptyView(R.layout.view_loading, (ViewGroup) recyclerView.getParent());
        mDisposable = RxRestClient.builder()
                .url(Url.GetCheckResult)
                .params("token", UserManager.getInstance().getToken())
                .params("repairId", repairId)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        List<CheckResultBean> list =
                                new ObjConverter<List<CheckResultBean>>().convert(response
                                        , new TypeReference<List<CheckResultBean>>() {
                                        });
                        adapter.setEmptyView(R.layout.view_search_empty,
                                (ViewGroup) recyclerView.getParent());
                        adapter.setNewData(list);
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {

                    }
                })
                .build()
                .post();
        setContentView(view);
    }

    private class ResultAdapter extends BaseQuickAdapter<CheckResultBean, BaseViewHolder> {

        public ResultAdapter(int layoutResId, @Nullable List<CheckResultBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, CheckResultBean item) {
            helper.setText(R.id.tv_result, item.getTime() + "  " + item.getResult());
            helper.setText(R.id.tv_content, item.getComment());
            helper.setText(R.id.tv_classification, "系别:" + item.getVehicleSeries());
            helper.setVisible(R.id.tv_classification, item.getType() == 0);
        }
    }
    public Dialog measureSize() {
        Window dialogWindow = getWindow();
        if (dialogWindow != null) {
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.width = ScreenUtils.getScreenWidth() * 800 / 1280; // 宽度
            lp.height = ScreenUtils.getScreenHeight() * 600 / 1024; // 高度
            dialogWindow.setGravity(Gravity.CENTER);
            dialogWindow.setAttributes(lp);
        }
        return this;
    }
    @Override
    public void dismiss() {
        if (mDisposable != null) {
            mDisposable.dispose();
        }
        super.dismiss();
    }
}
